<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:replace="~{inc/layout :: layout(~{::title}, ~{::section}, ~{::style}, ~{::script})}">
<head>
    <title>编辑问题</title>
    <style></style>
</head>
<body>
<section>
    <div class="container">
        <div class="row">
            <div class="col">

                <div class="card">
                    <div class="card-header" th:text="${question.getTitle()}"></div>
                    <div class="card-body">
                        <form onsubmit="doSubmit();return false;">
                            <textarea name="content2" id="content" rows="5" th:text="${answer.getContent()}"></textarea>
                            <button type="submit" class="btn btn-primary">提交</button>
                        </form>
                    </div>
                </div>


            </div>
        </div>

        <div>Included on page</div>
    </div>
</section>
<script th:inline="javascript">
    var questionId = [[${question.getId()}]];

    function doSubmit() {
        var answerId = [[${answer.getId()}]];

        var data = {id: answerId, content: $('#content').val()};
        $.ajax({
            url: '/api/answer/update',
            method: 'POST',
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify(data),
            success: function (resp) {
                console.info(resp);
                if (resp.code === 200) {
                    window.location.href = "/q/" + questionId;
                } else {
                    alert(resp.message);
                }
            },
            error: function (e) {
                console.info('ajax 请求出错了！');
                console.info(e);
            }
        });
    }
</script>
</body>
</html>